package com.xy.xymall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xy.xymall.product.entity.ProductAttrValueEntity;
import com.xy.xymall.product.service.CategoryService;
import com.xy.xymall.product.service.ProductAttrValueService;
import com.xy.xymall.product.vo.AttrRespVo;
import com.xy.xymall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xy.xymall.product.entity.AttrEntity;
import com.xy.xymall.product.service.AttrService;
import com.xy.common.utils.PageUtils;
import com.xy.common.utils.R;


/**
 * 商品属性
 *
 * @author XY
 * @email 2447315136@qq.com
 * @date 2020-04-11 10:30:32
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 修改规格参数
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> list = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data", list);
    }

    /**
     * 根据入参类型查询规格参数或者销售属性,包含模糊查询
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId, @PathVariable("attrType") String attrType) {

        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);

        AttrEntity attrEntity = attrService.getById(attrId);
        Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
        AttrRespVo respVo = attrService.getAttrInfo(attrId, catelogPath);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr) {
        //既要保存基本信息，又要保存关联关系
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 修改spu规格参数
     */
    @PostMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:update")
    public R update(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> list) {
        productAttrValueService.updateSpuAttr(spuId, list);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
